package virtPresenter.model
{
	public final class AS_Math
	{
		
		private static var locator:virtPresenterLocator = virtPresenterLocator.getInstance();
		
		public function AS_Math()
		{
			throw new Error("Just a static class. Instanciation not permitted.");	
		}
		/**
		 * Calculates the rise of an linear equation defined by two coordinates
		 */
		public static function riseOnLinearEquation(x1:Number, x2:Number, y1:Number, y2:Number):Number{
			var dx:Number = x1-x2;
			var dy:Number = y1-y2;
			var rise:Number = dx / dy;
			return rise;
		}
		
		public static function calculateValueOfLinearEquation(x:Number, rise:Number):Number{
			return x*rise;
		}
		
		public static function calculateNewThumbPosition(thumbX:int,  newX:int, y:int, leftLine:int, rightLine:int, leftAnchorPoint:int, rightAnchorPoint:int):int{
			return calculateNewThumbPositionPrecise(thumbX, newX, y, leftLine, rightLine, leftAnchorPoint, rightAnchorPoint); //relativ
		}
		
		public static function calculateNewThumbPositionPrecise(thumbX:int,  newX:int, y:int, leftLine:int, rightLine:int, leftAnchorPoint:int, rightAnchorPoint:int):Number{
			var leftBorderXPos:Number = calculateBorderLineXPos(y-AS_Constants.SCRUBBER_HEIGHT, leftLine, leftAnchorPoint);
			var rightBorderXPos:Number = calculateBorderLineXPos(y-AS_Constants.SCRUBBER_HEIGHT,rightLine, rightAnchorPoint);
			var currentWidth:Number = rightBorderXPos-leftBorderXPos;
			var newVideoPosition:Number = (thumbX-leftBorderXPos+newX)/currentWidth;
			var newThumbPosition:Number = leftLine - thumbX + (rightLine-leftLine)*newVideoPosition;
			return newThumbPosition; //relativ
		}
		
		public static function calculateBorderLineXPos(height:int, posOnScrubber:int, posOnPB:int):Number{	
			var rise:Number = AS_Math.riseOnLinearEquation(posOnScrubber,posOnPB,locator.scrubber.distanceScrubberToPreview(),0);
			var goal:Number = AS_Math.calculateValueOfLinearEquation(height,rise);
			return posOnScrubber-goal;
		}
		
		public static function calculateAnimLineValues(x:Number, animStepsXPos:Array, scrubberWidth:Number):Array{
			var lowest:Number,lowstep:Number,highstep:Number,highest:Number; 
			var countVar:int = 0;
			while (animStepsXPos[countVar]<=x)
				countVar++;
			if (animStepsXPos.length-countVar>=2){
				highstep = animStepsXPos[countVar];
				highest = animStepsXPos[countVar+1];
			} else if (animStepsXPos.length-countVar>=1){				
				highstep = animStepsXPos[countVar];	
				highest = scrubberWidth;
			} else if(countVar == animStepsXPos.length-1){
				highstep = highest = scrubberWidth;
			}
			if (countVar>=2){
				lowest = animStepsXPos[countVar-2];
				lowstep = animStepsXPos[countVar-1];
			} else if (countVar==1){
				lowstep = animStepsXPos[countVar-1];
				lowest = 0;
			}	
			return new Array(lowest, highest, lowstep, highstep);
		}
		
		public static function calculateAnimStepsXCoordiantes(scrubberWidth:Number):Array{
			var returnArray:Array = new Array();
			var animSteps:Array = locator.xmlParser.getTimeSteps();
			for (var i:int=0; i<animSteps.length;i++){
				var tmp:TimeStep = animSteps[i];
				returnArray.push(scrubberWidth*((tmp.getTime() / 1000) / (locator.metaData.duration)));
			}	
			returnArray.push(scrubberWidth);
			return returnArray;
		}
	}
}